<template>
    <div class="big_box">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>采购管理</el-breadcrumb-item>
            <el-breadcrumb-item>采购列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
        <el-form :model="selectfrom" ref="_selectfrom">
            <el-row :gutter="5">
                <el-col :span="3">
                    <el-input placeholder="采购单号" v-model="selectfrom.purchase.PurchaseNumber"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-input placeholder="物资名称" v-model="selectfrom.DeviceName"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-input placeholder="物资编号" v-model="selectfrom.DeviceNumber"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-date-picker style="width: 100%" placeholder="开始时间"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    v-model="selectfrom.Start"></el-date-picker>
                </el-col>
                <el-col :span="3">
                    <el-date-picker style="width: 100%" placeholder="结束时间"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    v-model="selectfrom.End"></el-date-picker>
                </el-col>
                <el-col :span="3">
                    <el-select placeholder="请选择部门" filterable v-model="selectfrom.purchase.DepartmentId">
                        <el-option :value="0" label="请选择部门"></el-option>
                        <el-option v-for="v in AllDepartment" :value="v.Id" :label="v.DepartmentName"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="3">
                    <el-select placeholder="请选择项目" filterable v-model="selectfrom.purchase.ProjectId">
                        <el-option :value="0" label="请选择项目"></el-option>
                        <el-option v-for="v in AllProject" :value="v.Id" :label="v.ProjectName"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="3">
                    <el-select placeholder="付款方式" v-model="selectfrom.purchase.PayMethod">
                        <el-option :value="-1" label="付款方式"></el-option>
                        <el-option :value="0" label="借款"></el-option>
                        <el-option :value="1" label="对公账户"></el-option>
                        <el-option :value="2" label="电子账户"></el-option>
                        <el-option :value="4" label="备用金"></el-option>
                        <el-option :value="8" label="垫付"></el-option>
                    </el-select>
                </el-col>
            </el-row>
            <el-row :gutter="5" style="padding:10px 0px">
                <el-col :span="3">
                    <el-select placeholder="采购单状态" v-model="selectfrom.purchase.State">
                        <el-option :value="-1" label="单据状态"></el-option>
                        <el-option :value="0" label="待签批"></el-option>
                        <el-option :value="1" label="采购确认"></el-option>
                        <el-option :value="2" label="运营综合部签批"></el-option>
                        <el-option :value="4" label="总经理签批"></el-option>
                        <el-option :value="128" label="部分入库"></el-option>
                        <el-option :value="256" label="全部入库"></el-option>
                        <el-option :value="512" label="已采购"></el-option>
                        <el-option :value="1024" label="已到货"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="3">
                    <el-select placeholder="供应商姓名" filterable v-model="selectfrom.purchase.VendorInf">
                        <el-option :value="0" label="请选择供应商姓名"></el-option>
                        <el-option v-for="d in AllCustomer" :label="d.CustomerName" :value="d.Id"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="3">
                    <el-select placeholder="供应商公司名称" filterable v-model="selectfrom.CustomerCompany">
                        <el-option :value="0" label="请选择供应商公司名称"></el-option>
                        <el-option v-for="d in AllCustomer" :label="d.CustomerCompany" :value="d.Id"></el-option>
                    </el-select>
                </el-col>
                <!--<el-col :span="2.5" style="line-height: 38px">
                    <el-switch inactive-text="我未审核" v-model="selectfrom.NoChecked"></el-switch>
                </el-col>-->
                <el-col :span="1.5">
                    <el-checkbox style="width:100%" v-model="selectfrom.NoChecked" label="我未审核" border></el-checkbox>
                </el-col>
                <el-col :span="2">
                    <el-button plain type="success" icon="el-icon-search" @click="formdata"></el-button>
                </el-col>
            </el-row>
        </el-form>
        <el-row style="padding-bottom: 10px">
            <el-col>
                <el-button-group>
                    <el-button class="buttongroup" type="danger" icon="el-icon-delete" @click="BatchDelete_event">批量删除
                    </el-button>
                    <el-button class="buttongroup" type="warning" icon="el-icon-delete" @click="BtnInboundAll_event">
                        批量入库
                    </el-button>
                    <el-button class="buttongroup" type="success" icon="el-icon-circle-plus-outline"
                               @click="AddPurchase_dialog=true">添加采购
                    </el-button>
                    <el-button class="buttongroup" type="success" icon="el-icon-circle-plus-outline" @click="onPays">
                        批量支付
                    </el-button>
                </el-button-group>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
                <el-table :data="tableData" border stripe style="width: 100%" @selection-change="SelectionChange_event">
                    <el-table-column type="selection" width="40" align="center"></el-table-column>
                    <el-table-column fixed type="index" label="序号" width="50" align="center"
                                     :index="indexMethod"></el-table-column>
                    <el-table-column fixed prop="PurchaseNumber" label="编号" align="center" show-overflow-tooltip
                                     width="120"></el-table-column>
                    <el-table-column prop="DepartmentId" label="申购部门" align="center" show-overflow-tooltip width="120">
                        <template slot-scope="slot">
                            {{ GetDepartmentName(slot.row.DepartmentId) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="TotalPrice" label="总金额" show-overflow-tooltip
                                     align="center"></el-table-column>
                    <el-table-column prop="PurchaseUserId" label="采购人" show-overflow-tooltip align="center" width="120">
                        <template slot-scope="slot">
                            {{ GetUserName(slot.row.PurchaseUserId) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="PurchaseDate" label="采购日期" show-overflow-tooltip align="center" width="120">
                        <template slot-scope="slot">
                            {{ fmtDate(slot.row.PurchaseDate) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="PayMethod" label="付款方式" show-overflow-tooltip align="center">
                        <template slot-scope="slot">
                            <label v-if="slot.row.PayMethod==0">借款</label>
                            <label v-if="slot.row.PayMethod==1">对公</label>
                            <label v-if="slot.row.PayMethod==2">电子账户</label>
                            <label v-if="slot.row.PayMethod==4">备用金</label>
                            <label v-if="slot.row.PayMethod==8">垫付</label>
                        </template>
                    </el-table-column>
                    <el-table-column prop="State" label="状态" show-overflow-tooltip align="center" width="120">
                        <template slot-scope="slot">
                            <div v-html="InitStateColum(slot.row)"></div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="ProjectId" label="项目" show-overflow-tooltip align="center" width="120">
                        <template slot-scope="slot">
                            {{ GetProjectName(slot.row.ProjectId) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="IsInvoice" label="是否有发票" show-overflow-tooltip align="center" width="120">
                        <template slot-scope="slot">
                            {{ slot.row.IsInvoice ? "是" : "否" }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="PurchaseType" label="采购类型" show-overflow-tooltip align="center" width="120">
                        <template slot-scope="slot">
                            <label v-if="slot.row.PurchaseType===0"></label>
                            <label v-if="slot.row.PurchaseType===1">办公用品</label>
                            <label v-if="slot.row.PurchaseType===2">生产原料</label>
                            <label v-if="slot.row.PurchaseType===4">仪器设备</label>
                            <label v-if="slot.row.PurchaseType===8">其他</label>
                        </template>
                    </el-table-column>
<!--                    <el-table-column prop="ReferenceWebsite" label="参考网址" show-overflow-tooltip align="center"-->
<!--                                     width="120">-->
<!--                    </el-table-column>-->
                    <el-table-column prop="AdvanceParty" label="垫付人" show-overflow-tooltip align="center">
                        <template slot-scope="slot">
                            {{ GetUserName(slot.row.AdvanceParty) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="PurchaseCause" label="用途说明" show-overflow-tooltip
                                     align="center"></el-table-column>
                    <el-table-column prop="VendorInf" label="供货商信息" show-overflow-tooltip align="center" width="120">
                        <template slot-scope="slot">
                            {{ GetCustomerName(slot.row.VendorInf) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="ApplyUserId" label="申购人" show-overflow-tooltip align="center" width="120">
                        <template slot-scope="slot">
                            {{ GetUserName(slot.row.ApplyUserId) }}
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="425" align="center">
                        <template slot-scope="scope">
                            <el-button plain type="warning" size="mini" @click="PurchaseOrder_event(scope.row)">采购单
                            </el-button>
                            <el-button plain type="primary" size="mini" @click="PurchaseGoodInbound_event(scope.row)">
                                入库
                            </el-button>
                            <el-button plain type="info" size="mini" @click="handleClick(scope.row,true)">查看</el-button>
                            <el-button plain @click="handleClick(scope.row,false)" type="success" size="mini">编辑
                            </el-button>
                            <el-button plain @click="ExpressDialogEvent(scope.row)" type="success" size="mini">快递
                            </el-button>
                            <el-button plain type="danger" size="mini" @click="BatchDeleteOne_event(scope.row)">删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
            <el-col :span="24">
                <div class="block">
                    <el-pagination
                            @size-change="formdata"
                            @current-change="formdata"
                            :current-page.sync="pagination.currentPage"
                            :page-sizes="pagination.pagesizes"
                            :page-size.sync="pagination.pagesize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total.sync="pagination.total">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>

        <el-dialog :visible.sync="AddPurchase_dialog" width="85%" title="添加采购" top="10px"
                   @close="AddPurchase_close_event">
            <AddPurchase ref="AddPurchase_refs" :all-customer="AllCustomer" :all-project="AllProject"
                         :all-department="AllDepartment" :all-device-category="AllDeviceCategory"
                         :all-device-state="AllDeviceState" :all-express="ExpressList"></AddPurchase>
        </el-dialog>

        <el-dialog :visible.sync="UpdatePurchase_dialog.dialog" width="85%" top="10px"
                   :title="'修改采购---'+UpdatePurchase_dialog.title">
            <UpdatePurchase :all-customer="AllCustomer" :all-project="AllProject" :is-see="IsSee"
                            :all-department="AllDepartment" :all-device-category="AllDeviceCategory"
                            :purchase="Purchase_details"
                            :all-device-state="AllDeviceState" :all-express="ExpressList"></UpdatePurchase>
        </el-dialog>

        <el-dialog :visible.sync="Table_Purchase.dialog" fullscreen :title="'采购单---'+Table_Purchase.title">
            <PurchaseTable :audit-rule="auditRule" :all-department="AllDepartment" :purchase="Purchase_details_"
                           :all-project="AllProject" :all-customer="AllCustomer"></PurchaseTable>
        </el-dialog>

        <el-dialog :visible.sync="PurchaseGoodInbound_dialog" title="入库" top="10px" width="85%">
            <PurchaseGoodInbound :table-data="PurchaseGoodsLis"></PurchaseGoodInbound>
        </el-dialog>

        <el-dialog width="60%" :visible.sync="Express.show" :title="'快递单号: '+Express.title">
            <el-row :gutter="5" style="margin-bottom: 20px;">
                <el-col :span="6">
                    <el-select style="width: 100%" filterable v-model="Express.obj.ExpressKey">
                        <el-option v-for="ex in ExpressList" :value="ex.Key" :label="ex.Name"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="6">
                    <el-input v-model="Express.obj.ExpressNumber"></el-input>
                </el-col>
                <el-col :span="1">
                    <el-button icon="el-icon-search" @click="ExpressSelectEvent(Express.obj)"></el-button>
                </el-col>
            </el-row>
            <el-timeline>
                <el-timeline-item
                        v-for="(d, index) in Express.data"
                        :key="index"
                        :timestamp="d.time">
                    {{d.context}}
                </el-timeline-item>
            </el-timeline>
        </el-dialog>

    </div>
</template>

<script>
    import * as publicjs from "../../../../public/Scriput/Public";
    import AddPurchase from "./AddPurchase";
    import UpdatePurchase from "./UpdatePurchase";
    import PurchaseTable from "./PurchaseTable";
    import PurchaseGoodInbound from "./PurchaseGoodInbound";
    import $ from 'jquery'

    export default {
        name: "Purchase",
        components: {
            'AddPurchase': AddPurchase,
            'UpdatePurchase': UpdatePurchase,
            'PurchaseTable': PurchaseTable,
            'PurchaseGoodInbound': PurchaseGoodInbound,
        },
        data() {
            return {
                //表格查询数据
                selectfrom: {
                    purchase: {
                        DepartmentId: 0,
                        ProjectId: 0,
                        PurchaseNumber: '',
                        State: -1,
                        PayMethod: -1,
                        VendorInf: 0,
                    },
                    DeviceName: '',
                    DeviceNumber: '',
                    Start: '',
                    End: '',
                    NoChecked: false,
                    CustomerCompany: 0,
                },
                //表格数据
                tableData: [],
                //分页数据绑定
                pagination: {
                    currentPage: 1,
                    pagesizes: [10, 20, 30, 40, 50],
                    pagesize: 10,
                    total: 100
                },
                //多选列表的数据集合
                SelectMore_lis: [],
                //所有部门数据
                AllDepartment: [],
                //物品状态
                AllDeviceState: [],
                //所有项目
                AllProject: [],
                // 供应商信息
                AllCustomer: [],
                //所有物资分类
                AllDeviceCategory: [],
                // 添加采购弹框
                AddPurchase_dialog: false,
                // 修改弹框
                UpdatePurchase_dialog: {
                    dialog: false,
                    title: '',
                },
                // 单条数据详情
                Purchase_details: {},
                // 单条数据详情(表格数据)
                Purchase_details_: {},
                //编辑/查看
                IsSee: false,
                Table_Purchase: {
                    dialog: false,
                    title: '',
                },
                //审批数据
                auditRule: {},
                // 入库单弹框页面
                PurchaseGoodInbound_dialog: false,
                // 批量入库页面表格数据
                PurchaseGoodsLis: [],
                //快递公司数据
                ExpressList: [],
                //快递框显示
                Express: {
                    show: false,
                    title: '',
                    obj: '',
                    data: [],
                    IsExpress: false,
                },
            }
        },
        created() {
            this.Init()
            this.formdata()
        },
        methods: {
            //初始数据加载
            Init() {
                this.AllProject = publicjs.GetAllProject(this.$http)
                this.AllDepartment = publicjs.GetAllDepartment(this.$http)
                this.AllDeviceState = publicjs.GetAllDeviceState(this.$http)
                this.AllCustomer = publicjs.GetAllCustomer(this.$http)
                this.AllDeviceCategory = publicjs.GetAllDeviceCategory(this.$http)
                this.ExpressList = publicjs.GetAllExpressCompany(this.$http)
            },
            //表格数据序列号
            indexMethod(index) {
                return ((this.pagination.currentPage - 1) * this.pagination.pagesize) + index + 1
            },
            //初始化表格数据加载
            formdata() {
                let loadding = this.openLoading();
                this.$http.post('Purchase/GetPurchaseDetail', {
                    currentPage: this.pagination.currentPage,
                    pagesize: this.pagination.pagesize,
                    Start: this.selectfrom.Start,
                    End: this.selectfrom.End,
                    NoChecked: this.selectfrom.NoChecked,
                    DeviceName: this.selectfrom.DeviceName,
                    DeviceNumber: this.selectfrom.DeviceNumber,
                    purchase: this.selectfrom.purchase,
                    CustomerId: this.selectfrom.CustomerCompany,
                }).then(res => {
                    loadding.close();
                    this.pagination.total = res.data.count
                    this.tableData = res.data.data
                }).catch(() => {
                    loadding.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //表格单个删除
            BatchDeleteOne_event(obj) {
                if (this.$store.state.user.id === +obj.PurchaseUserId) {
                    this.$http.post('Purchase/IsPowerPurchaseById', [+obj.PurchaseUserId]).then(res => {
                        if (res.data.Status) {
                            this.$http.get('Purchase/ValidateState?data=' + obj.PurchaseNumber).then(res => {
                                if (!res.data.IsSuccess) {
                                    this.$message.warning(res.data.Msg)
                                    return false
                                } else {
                                    this.SelectMore_lis.push(obj)
                                    this.BatchDelete_event()
                                }
                            })
                        } else {
                            this.$message.warning('请选择同一采购人进行操作！')
                        }
                    })
                } else {
                    this.$message.warning('请选择同一采购人进行操作！')
                }
            },
            //编辑
            handleClick(obj, Isshow) {
                if (this.$store.state.user.id === +obj.PurchaseUserId && !Isshow) {
                    this.$http.post('Purchase/IsPowerPurchaseById', [+obj.PurchaseUserId]).then(res => {
                        if (res.data.Status) {
                            let newdata = JSON.stringify(obj)
                            this.Purchase_details = JSON.parse(newdata)
                            this.UpdatePurchase_dialog.dialog = true
                            this.UpdatePurchase_dialog.title = obj.PurchaseNumber
                            this.IsSee = Isshow
                        } else {
                            this.$message.warning('请选择同一采购人进行操作！')
                        }
                    })
                }
                else if(Isshow){
                    let newdata = JSON.stringify(obj)
                    this.Purchase_details = JSON.parse(newdata)
                    this.UpdatePurchase_dialog.dialog = true
                    this.UpdatePurchase_dialog.title = obj.PurchaseNumber
                    this.IsSee = Isshow
                }
                else {
                    this.$message.warning('请选择同一采购人进行操作！')
                }
            },
            //快递弹框打开
            ExpressDialogEvent(obj) {
                this.Express.obj = obj
                if ((obj.ExpressKey == '' || obj.ExpressKey == null) || (obj.ExpressNumber == '' || obj.ExpressNumber == null)) {
                    this.Express.data = []
                    this.Express.title = []
                    this.Express.IsExpress = true
                } else {
                    this.ExpressSelectEvent(obj)
                }
                this.Express.show = true
            },
            //快递查询
            ExpressSelectEvent(obj) {
                let _this = this
                $.ajax({
                    type: 'get',
                    url: "http://api.kuaidi.com/openapi.html?id=95f0845b1b31bdc95670f5e6104c0e11&com=" + obj.ExpressKey + "&nu=" + obj.ExpressNumber,
                    success: function (res) {
                        _this.Express.data = res.data
                        _this.Express.title = obj.ExpressNumber
                        //防止原有数据的重复提交
                        if (_this.Express.IsExpress) {
                            _this.$http.post("purchase/AddExpressDetails", {purchase: obj}).then(res => {
                                if (res.data.IsSuccess) {
                                    _this.$message.success(res.data.Msg)
                                } else {
                                    _this.$message.warning(res.data.Msg)
                                }
                            })
                            _this.Express.IsExpress = false
                        }
                    },
                    error: function () {
                        _this.$message.warning('没有相关的快递信息!')
                    }
                })
            },
            //批量删除
            BatchDelete_event() {
                if (!this.SelectMore_lis.length) {
                    this.$message.warning('您还未选择需要删除的采购!')
                } else {
                    this.$confirm('你确定要删除吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'error'
                    }).then(() => {
                        let allPurchaseUserId = [],PurchaseNumber = [];
                        for(let i = 0; i < this.SelectMore_lis.length; i++){
                            if(allPurchaseUserId.indexOf(+this.SelectMore_lis[i].PurchaseUserId) < 0){
                                if (this.$store.state.user.id !== +this.SelectMore_lis[i].PurchaseUserId) {
                                    this.$message.warning('请选择同一采购人进行操作！');
                                    return;
                                }
                                allPurchaseUserId.push(+this.SelectMore_lis[i].PurchaseUserId)
                            }
                            PurchaseNumber.push(this.SelectMore_lis[i].PurchaseNumber)
                        }
                        this.$http.post('Purchase/IsPowerPurchaseById', allPurchaseUserId).then(res => {
                            if (res.data.Status) {
                                let loadding = this.openLoading();
                                this.$http.post('Purchase/DeletePurchaseContent', PurchaseNumber).then(res => {
                                    loadding.close();
                                    if (res.data.IsSuccess) {
                                        this.$message.success(res.data.Msg)
                                        this.formdata()
                                        this.SelectMore_lis = []
                                    } else {
                                        this.$message.warning(res.data.Msg)
                                    }
                                }).catch(() => {
                                    loadding.close();
                                    this.$message.error('服务器异常,请联系管理员!')
                                })
                            } else {
                                this.$message.warning('请选择同一采购人进行操作！')
                            }
                        })
                    }).catch(() => {
                        this.$message.info('已取消!')
                        this.SelectMore_lis = []
                    })

                }
            },
            // 批量支付
            onPays() {
                if (!this.SelectMore_lis.length) {
                    this.$message.warning('请选择采购单!')
                } else {
                    this.$confirm('你确定要批量支付吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'success'
                    }).then(() => {
                        let data = [], PurchaseNumbers = [];
                        let isPays = true, msg = "";
                        for (let i = 0; i < this.SelectMore_lis.length; i++) {
                            //if (this.SelectMore_lis[i].FinanceloanNumber && !isNaN(this.SelectMore_lis[i].FinanceloanNumber)) {
                                //isPays = false;
                                //msg = "编号为：" + this.SelectMore_lis[i].PurchaseNumber + "的采购单已生成过支付单，不能再次生成!";
                            //}
                            if(+this.SelectMore_lis[i].PurchaseUserId !== +this.$store.state.user.id){
                                isPays = false;
                                msg = "请选择采购人为本人的采购单，错误编号为：" + this.SelectMore_lis[i].PurchaseNumber
                            }
                            // 支付方式
                            let PayMethod = -1;
                            if (this.SelectMore_lis[i].PayMethod === 0) {
                                PayMethod = this.SelectMore_lis[i].PurchaseUserId
                            } else if (this.SelectMore_lis[i].PayMethod === 1) {
                                if (!this.SelectMore_lis[i].VendorInf && this.SelectMore_lis[i].VendorInf !== 0) {
                                    isPays = false;
                                    msg = "对公支付方式没有供货商！";
                                    break;
                                } else {
                                    PayMethod = this.SelectMore_lis[i].VendorInf;
                                }
                            } else {
                                isPays = false;
                                msg = "不支持除对公和借款外其他支付方式！";
                                break;
                            }
                            // 支付对象
                            let CustomerCompany = "";
                            if (!this.SelectMore_lis[i].VendorInf) {
                                CustomerCompany = this.SelectMore_lis[i].PurchaseUserId;
                            } else {
                                CustomerCompany = this.SelectMore_lis[i].VendorInf;
                            }
                            // 项目
                            let ProjectId = "";
                            if (!this.SelectMore_lis[i].ProjectId) {
                                ProjectId = i + "ProjectId";
                            } else {
                                ProjectId = this.SelectMore_lis[i].ProjectId;
                            }
                            // 部门
                            let DepartmentId = this.SelectMore_lis[i].DepartmentId;
                            if (data.length) {
                                for (let l = 0; l < data.length; l++) {
                                    if (data[l][data[l].length - 1].CustomerCompany === CustomerCompany &&
                                        data[l][data[l].length - 1].ProjectId === ProjectId &&
                                        data[l][data[l].length - 1].PayMethod === PayMethod &&
                                        data[l][data[l].length - 1].DepartmentId === DepartmentId
                                    ) {
                                        data[l].push({
                                            CustomerCompany: CustomerCompany,
                                            ProjectId: ProjectId,
                                            PayMethod: PayMethod,
                                            DepartmentId: DepartmentId,
                                        })
                                        PurchaseNumbers[l].push(this.SelectMore_lis[i].PurchaseNumber);
                                        break;
                                    } else if (l === data.length - 1) {
                                        data.push([{
                                            CustomerCompany: CustomerCompany,
                                            ProjectId: ProjectId,
                                            PayMethod: PayMethod,
                                            DepartmentId: DepartmentId,
                                        }])
                                        PurchaseNumbers.push([this.SelectMore_lis[i].PurchaseNumber]);
                                        break;
                                    }
                                }
                            } else {
                                data.push([
                                    {
                                        CustomerCompany: CustomerCompany,
                                        ProjectId: ProjectId,
                                        PayMethod: PayMethod,
                                        DepartmentId: DepartmentId,
                                    }
                                ])
                                PurchaseNumbers.push([this.SelectMore_lis[i].PurchaseNumber])
                            }
                        }
                        if (!isPays) {
                            this.$message.error(msg);
                            return;
                        }
                        let loadding = this.openLoading();
                        this.$http.post('Purchase/AddFinanceLoanByPur', PurchaseNumbers).then(res => {
                            if (res.data.Status) {
                                this.$message.success(res.data.Message)
                            } else {
                                this.$message.error(res.data.Message)
                            }
                            this.formdata()
                            loadding.close();
                        }).catch(() => {
                            loadding.close();
                            this.$message.error('服务器异常,请联系管理员!')
                        })
                    }).catch(() => {

                    })
                }
            },
            //事件转化
            fmtDate(time) {
                return time.split(' ')[0];
            },
            //获取对应人员名字
            GetUserName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.$store.state.AllUsers.length; i++) {
                        if (this.$store.state.AllUsers[i].Id == value) {
                            name = this.$store.state.AllUsers[i].UserName
                            return name
                        }
                    }
                }
            },
            //获取对应部门名字
            GetDepartmentName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.AllDepartment.length; i++) {
                        if (this.AllDepartment[i].Id == value) {
                            name = this.AllDepartment[i].DepartmentName
                            return name
                        }
                    }
                }
            },
            //获取对应项目名字
            GetProjectName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.AllProject.length; i++) {
                        if (this.AllProject[i].Id == value) {
                            name = this.AllProject[i].ProjectName
                            return name
                        }
                    }
                }
            },
            //获取对应项目名字
            GetCustomerName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.AllCustomer.length; i++) {
                        if (this.AllCustomer[i].Id == value) {
                            name = this.AllCustomer[i].CustomerName
                            return name
                        }
                    }
                }
            },
            //状态
            InitStateColum(d) {
                // return publicjs.InitStateColum(obj)
                let result = ''
                switch (d.State) {
                    case -1:
                        result = "<span>草稿</span>"
                        break;
                    case 0:
                        result = "<span>待审批</span>"
                        break;
                    case 1:
                        result = "<span style='color:#1E90FF'>采购确认</span>"
                        break;
                    case 2:
                        if (d.IsRefuse) {
                            result = "<span style='color:#FF5722'>第一级已拒绝</span>";
                        } else {
                            result = "<span style='color:#1E90FF'>第一级已同意</span>";
                        }
                        break;
                    case 4:
                        if (d.IsRefuse) {
                            result = "<span style='color:#FF5722'>第二级已拒绝</span>";
                        } else {
                            result = "<span style='color:#7CFC00'>第二级已同意</span>";
                        }
                        break;
                    case 8:
                        if (d.IsRefuse) {
                            result = "<span style='color:#FF5722'>第三级已拒绝</span>";
                        } else {
                            result = "<span style='color:#5e09f5'>第三级已同意</span>";
                        }
                        break;
                    case 16:
                        if (d.IsRefuse) {
                            result = "<span style='color:#FF5722'>第四级已拒绝</span>";
                        } else {
                            result = "<span style='color:#009688'>第四级已同意</span>";
                        }
                        break;
                    case 32:
                        if (d.IsRefuse) {
                            result = "<span style='color:#FF5722'>第五级已拒绝</span>";
                        } else {
                            result = "<span style='color:#108ae4'>第五级已同意</span>";
                        }
                        break;
                    case 64:
                        if (d.IsRefuse) {
                            result = "<span style='color:#FF5722'>第六级已拒绝</span>";
                        } else {
                            result = "<span style='color:#fbb80a'>出六已同意</span>";
                        }
                        break;
                    case 128:
                        result = "<span style='color:#551A8B'>部分入库</span>";
                        break;
                    case 256:
                        result = "<span style='color:#0000CD'>全部入库</span>";
                        break;
                    case 512:
                        result = "<span style='color:#00CD28'>已采购</span>";
                        break;
                    case 1024:
                        result = "<span style='color:#00CD28'>已到货</span>";
                        break;
                }
                return result
            },
            //表格数据选择事件
            SelectionChange_event(value) {
                this.SelectMore_lis = value
            },
            //采购单审批弹框
            PurchaseOrder_event(obj) {
                this.$http.get('Report/PurchaseOrder?PurchaseNumber=' + obj.PurchaseNumber).then(res => {
                    if (res.data.obj != null) {
                        this.auditRule = res.data.auditRule
                        this.Purchase_details_ = res.data.obj
                        this.Table_Purchase.dialog = true
                        this.Table_Purchase.title = obj.PurchaseNumber
                    }
                })
            },
            //入库单入库事件
            PurchaseGoodInbound_event(obj) {
                if (this.$store.state.user.id === +obj.PurchaseUserId) {
                    this.$http.post('Purchase/IsPowerPurchaseById', [+obj.PurchaseUserId]).then(res => {
                        if (res.data.Status) {
                            this.SelectMore_lis.length = 0
                            this.SelectMore_lis.push(obj)
                            this.BtnInboundAll_event()
                        } else {
                            this.$message.warning('请选择同一采购人进行操作！')
                        }
                    })
                } else {
                    this.$message.warning('请选择同一采购人进行操作！')
                }
            },
            //批量入库
            BtnInboundAll_event() {
                if (this.SelectMore_lis.length === 0) {
                    this.$message.warning('您还未选择任何采购记录')
                    return
                }
                let allPurchaseUserId = [],PurchaseNumber = [];
                for(let i = 0; i < this.SelectMore_lis.length; i++){
                    if(allPurchaseUserId.indexOf(+this.SelectMore_lis[i].PurchaseUserId) < 0){
                        if (this.$store.state.user.id !== +this.SelectMore_lis[i].PurchaseUserId) {
                            this.$message.warning('请选择同一采购人进行操作！');
                            return;
                        }
                        allPurchaseUserId.push(+this.SelectMore_lis[i].PurchaseUserId)
                    }
                    PurchaseNumber.push(this.SelectMore_lis[i].PurchaseNumber)
                }
                this.$http.post('Purchase/IsPowerPurchaseById', allPurchaseUserId).then(res => {
                    if (res.data.Status) {
                        let loadding = this.openLoading();
                        this.$http.get('Purchase/PurchaseGoodInbound?PurchaseNumber=' + PurchaseNumber).then(res => {
                            loadding.close();
                            this.PurchaseGoodsLis = res.data
                            this.PurchaseGoodInbound_dialog = true
                        })
                    } else {
                        this.$message.warning('请选择同一采购人进行操作！')
                    }
                })
            },
            // 添加数据关闭回调
            AddPurchase_close_event() {
                this.$refs['AddPurchase_refs'].Fromreset()
            },
        }
    }
</script>

<style scoped>

</style>
